<?php
# Temporarily use an alternative full version class file.
if(is_file('D:/htdocs/smarty-framework/public_html/library/classes/sanjaal/class.cropnail.inc.php'))
{
	require_once('D:/htdocs/smarty-framework/public_html/library/classes/sanjaal/class.cropnail.inc.php');
}

/**
* @author Bimal Poudel
* @package Smarty Framework
* @support http://www.odesk.com/users/~~dd91d11caed0cdce
* @contact http://www.sanjaal.com
* @company Sanjaal Corps
*/

if(!class_exists(cropnail))
{
class cropnail
{
	/**
	* Resize the output image to these dimensions
	*/
	private $resize_width=0;
	private $resize_height=0;
	
	/**
	* Image output quality
	*/
	private $quality = 100;
	
	/**
	* Continue to other images during mass processing
	* even if an error was found at some point
	*/
	private $allow_continue;

	/**
	* Possible positions for resizes
	*/
	const CROPNAIL_VERTICAL_LEFT		= 1;
	const CROPNAIL_VERTICAL_CENTER		= 2;
	const CROPNAIL_VERTICAL_RIGHT		= 3;
	const CROPNAIL_HORIZONTAL_TOP		= 4;
	const CROPNAIL_HORIZONTAL_CENTER	= 5;
	const CROPNAIL_HORIZONTAL_BOTTOM	= 6;
	const CROPNAIL_BOTTOM_RIGHT		= 7;
	const CROPNAIL_BOTTOM_LEFT		= 8;
	const CROPNAIL_TOP_RIGHT		= 9;
	const CROPNAIL_TOP_LEFT			= 10;
	
	/**
	* List of messages for debug
	*/
	private $debug = array();
	
	/**
	* Define target image dimensions at the time of initiation
	*/
	public function __construct($width=0, $height=0)
	{
		$this->resize_width = abs((int)$width);
		$this->resize_height = abs((int)$height);
		$this->quality = 100;
		
		/**
		* Are the target dimensions ok?
		*/
		$this->allow_continue = $this->resize_width && $this->resize_height;
		
		$this->debug = array();
	}
	
	public function resize($original_filename='', $cropnail_filename='', $crop_pattern=0)
	{
		if(!$this->allow_continue) return false;
		
		/**
		* Find out the source image size
		*/
		$canvas_sizes = getimagesize($original_filename);
		if(!$canvas_sizes) { $this->debug[] = "Not an image: {$original_filename}"; return false; }
		
		# Original image
		$canvas_width = $canvas_sizes[0];
		$canvas_height = $canvas_sizes[1];

		# Maximum size of the clip propertional to the thumb nail
		$clip_width=0;
		$clip_height=0;
		
		# From where should we crop the clip
		$source_x1 = 0; $source_y1 = 0;
		$source_x2 = 0; $source_y2 = 0;
		
		if($canvas_width/$this->resize_width < $canvas_height/$this->resize_height)
		{
			$clip_width = $canvas_width;
			$clip_height = $this->resize_height * $canvas_width / $this->resize_width;
			$this->debug[] = "Section A";
			
			# CROPNAIL_HORIZONTAL_CENTER
			$source_x1 = 0; $source_y1 = ($canvas_height-$clip_height)/2;
		}
		else
		{
			$clip_width = $this->resize_width * $canvas_height / $this->resize_height;
			$clip_height = $canvas_height;
			$this->debug[] = "Section B";
			
			# CROPNAIL_VERTICAL_CENTER
			$source_x1 = ($canvas_width-$clip_width)/2; $source_y1 = 0;
		}
		$source_x2 = $source_x1+$clip_width; $source_y2 = $source_y1+$clip_height;
		
		# Validations
		$clip_width  = ceil($clip_width);
		$clip_height = ceil($clip_height);
		$source_x1 = ceil($source_x1); $source_y1 = ceil($source_y1);
		$source_x2 = ceil($source_x2); $source_y2 = ceil($source_y2);
		
		# Produce the croped thumbnail (actual cropnail)
		$destination = imagecreatetruecolor($this->resize_width, $this->resize_height);
		$source = imagecreatefromjpeg($original_filename);
		imagecopyresampled($destination, $source, 0, 0, $source_x1, $source_y1, $this->resize_width, $this->resize_height, $clip_width, $clip_height);
		imagejpeg($destination, $cropnail_filename, $this->quality);

		# Some debug information
		$this->debug[] = "Canvas: {$canvas_width} x {$canvas_height}, {$original_filename}";
		$this->debug[] = "Clip: {$clip_width} x {$clip_height}";
		$this->debug[] = "Resize: {$this->resize_width} x {$this->resize_height}";
		$this->debug[] = "Source (x1,y1), (x2,y2): ({$source_x1},{$source_y1}), ({$source_x2},{$source_y2})";
		
		return true;
	}
	
	/**
	* Print the debug message
	*/
	public function debug()
	{
		echo implode("\r\n", $this->debug);
	}
} # class cropnail
} # if()
?>